﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Helper;
using yiCommerce.Service.Loggings;
using yiCommerce.Service.Products;

namespace yiCommerce.Admin.Controllers.api
{
    public class ProductVariantAttributeApiController : ApiController
    {
        private IProductService _productService;
        private readonly IWorkContext _workContext;
        private IActivityService _activityService;

        public ProductVariantAttributeApiController(IProductService productService, IActivityService activityService, IWorkContext workContext)
        {
            _productService = productService;
            _activityService = activityService;
            _workContext = workContext;
        }

        public ProductVariantAttributeValueSaveResult InsertProductVariantAttributeValue(ProductVariantAttributeValueSaveRequest request)
        {
            var result = new ProductVariantAttributeValueSaveResult();
            try
            {
                var pvaId = _productService.InsertProductVariantAttributeValue(request.ProductVariantAttributeId, request.Value, request.DisplayOrder);


                result.Status = "0";
                result.Id = pvaId;

                _activityService.InsertActivity("InsertProductVariantAttributeValue", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
                return result;
            }
            catch (Exception e)
            {
                result.Status = "1";
                result.Message = e.ToString();
                return result;
            }
        }

        public ProductVariantAttributeSaveResult InsertProductVariantAttribute(
          ProductVariantAttributeSaveRequest request)
        {
            var result = new ProductVariantAttributeSaveResult();
            try
            {
                var pvaId =_productService.InsertProductVariantAttribute(request.PvId, (AttributeControlType)request.SelectedAttributeType.ControlTypeValue,request.SelectedProductAttribute.Id);


                result.Status = "0";
                result.Id = pvaId;

                _activityService.InsertActivity("InsertProductVariantInfo", JsonHelper.ToJson(request), _workContext.CurrentCustomer);
                return result;
            }
            catch (Exception e)
            {
                result.Status = "1";
                result.Message = e.ToString();
                return result;
            }
        }

    }

    public class ProductVariantAttributeValueSaveRequest
    {
        public int ProductVariantAttributeId { get; set; }
        public string Value { get; set; }
        public int DisplayOrder{ get; set; }
    }

    public class ProductVariantAttributeValueSaveResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }
}
